<?php

class Tasks {
	private $db;
	
	public function __construct() {
		$this->db = Db::getInstance ();
	}
	
	function getTasks($limit_start, $limit_count) {
		$stmt = $this->db->prepare("SELECT * FROM tasks ORDER by created DESC LIMIT ?,?");
		$stmt->bindParam(1,$limit_start,PDO::PARAM_INT);
		$stmt->bindParam(2,$limit_count,PDO::PARAM_INT);
		$stmt->execute();
		return $stmt->fetchAll(PDO::FETCH_ASSOC); 
	}
	
	function getTasksId($id){
		$stmt = $this->db->prepare("SELECT * FROM tasks WHERE id = ?");
		$stmt->bindParam(1,$id,PDO::PARAM_INT);
		$stmt->execute();
		
		return $stmt->fetch(PDO::FETCH_ASSOC);
	}
	
	function commentAdd($comment,$user_id,$task_id){
		$stmt = $this->db->prepare("INSERT INTO tasks_comment 
		(comment,user_id,task_id,created)
		VALUES
		(?,?,?,?);
		");
		
		$stmt->bindParam(1,$comment,PDO::PARAM_STR,200);
		$stmt->bindParam(2,$user_id,PDO::PARAM_INT);
		$stmt->bindParam(3,$task_id,PDO::PARAM_INT);
		$stmt->bindParam(4,date('Y:n:d H:i:s'),PDO::PARAM_STR);
		$stmt->execute();
	}
	
	function getComments($task_id) {
		$stmt = $this->db->prepare("SELECT tasks_comment.id,comment,user_id, tasks_comment.created, users.login AS user_login 
		FROM tasks_comment 
		JOIN users ON users.id = tasks_comment.user_id
		WHERE task_id = ?");
		$stmt->bindParam(1,$task_id,PDO::PARAM_INT);
		$stmt->execute();
		
		return $stmt->fetchAll(PDO::FETCH_ASSOC);
	}
	
	function getCountTasks() {
		$stmt = $this->db->query("SELECT * FROM tasks");
		return $stmt->rowCount();
	}
	
	
	function addTask($title,$text,$time_limit,$memory_limit,$points, $file) {
		$stmt = $this->db->prepare ( "INSERT INTO tasks 
		(title,text,time_limit,memory_limit,points, file, created)
		VALUES
		(?,?,?,?,?,?,?);
		" );
		
		//I know, it's bad tone..
		$stmt->bindParam ( 1, $title, PDO::PARAM_STR);
		$stmt->bindParam ( 2, $text, PDO::PARAM_STR);
		$stmt->bindParam ( 3, $time_limit, PDO::PARAM_INT);
		$stmt->bindParam ( 4, $memory_limit, PDO::PARAM_INT);
		$stmt->bindParam ( 5, $points, PDO::PARAM_INT);
		$stmt->bindParam ( 6, $file, PDO::PARAM_STR);
		$stmt->bindParam ( 7, date ( 'Y:n:d H:i:s' ), PDO::PARAM_STR );
		
		$stmt->execute ();
	}
}

?>